import json
import requests


def ku6_spider():

    for page in range(0,11):
        print('正在抓取第{}页数据'.format(page+1))
        #1.确定URL路径，headers参数
        base_url = 'https://www.ku6.com/video/feed'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
        }
        params = {
            'pageNo': str(page),
            'pageSize': '40',
            'subjectId': '76',
        }


        #2.模拟浏览器-requests发送请求获取数据
        response = requests.get(base_url,headers=headers,params=params)
        res_data = response.text
        #print(res_data)


        #3.解析数据
        json_data = json.loads(res_data)    #--字典

        data_list = json_data['data']

        #遍历列表
        for data in data_list:
            #print(data)
            video_name = data['title']+".mp4"
            video_url = data['playUrl']
            #print(video_url,video_name)

            print('下载中：',video_name)
            video_data = requests.get(video_url,headers=headers).content

            #4.保存数据
            with open('video\\'+video_name,'wb') as f:
                f.write(video_data)
                print('下载完成...')



if __name__ == '__main__':
    ku6_spider()